package com.ytf.auth.controller;

import com.ytf.core.common.Resp;
import com.ytf.core.web.application.UserAppService;
import com.ytf.core.web.pojo.dto.UserDto;
import com.ytf.core.web.pojo.vo.UserVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author YTF
 * @since 2025-05-20
 */
@Tag(name = "用户")
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserAppService userAppService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @PostMapping
    public Resp<Void> insert(@RequestBody UserDto userDto){
        Boolean aBoolean = userAppService.insert(userDto);
        return aBoolean ? Resp.success():Resp.error();
    }

    @Operation(summary = "删除用户")
    @DeleteMapping("/{id}")
    public Resp<Void> deleteById(@PathVariable @Schema String id){
        userAppService.deleteById(Long.valueOf(id));
        return Resp.success();
    }

    @Operation(summary = "删除用户")
    @PutMapping("/{id}")
    public Resp<Void> update(@PathVariable @Schema(name = "id") String id ,@RequestBody @Schema(implementation = UserDto.class) UserDto userDto){
        userDto.setPassword(passwordEncoder.encode(userDto.getPassword()));
        Boolean aBoolean = userAppService.updateById(Long.valueOf(id),userDto);
        return aBoolean ? Resp.success():Resp.error();
    }


    @Operation(summary = "查询用户")
    @GetMapping("/{id}")
    public Resp<UserVo> getById(@PathVariable @Schema String id){
        UserVo Vo = userAppService.getById(Long.valueOf(id));
        return Vo != null ? Resp.success(Vo):Resp.error();
    }

}
